package cn.snmp.mutual.nms;

public class SnmpOID extends SnmpVar
{
    int[] value;
    byte[] byteValue;
    private static int[] zeroDotZero;
    private static final char[] radixTenTenths;
    private static final char[] radixTenUnits;
    
    SnmpOID() {
        this.value = null;
        this.Type = 6;
    }
    
    public SnmpOID(final String s) {
        this.value = null;
        this.Type = 6;
        if (s == null) {
            return;
        }
        this.setOID(s);
    }
    
    public SnmpOID(final int[] ai) {
        this.value = null;
        this.Type = 6;
        System.arraycopy(ai, 0, this.value = new int[ai.length], 0, ai.length);
    }
    
    long[] convertIntArrayToLongArray(final int[] ai) {
        final long[] al = new long[ai.length];
        for (int i = 0; i < ai.length; ++i) {
            al[i] = ((long)ai[i] & 0xFFFFFFFFL);
        }
        return al;
    }
    
    @Override
    int encode(final byte[] abyte0, final int i) throws ArrayIndexOutOfBoundsException {
        final int[] ai = (this.value != null) ? this.value : SnmpOID.zeroDotZero;
        return ASNTypes.encodeOid(abyte0, i, ai, ai.length, 6);
    }
    
    @Override
    public boolean equals(final Object obj) {
        return obj != null && obj instanceof SnmpOID && SnmpAPI.compare(this.value, ((SnmpOID)obj).value);
    }
    
    private int getCharacterCount() {
        int i = 0;
        final int l = this.value.length;
        final byte byte0 = 48;
        for (int i2 = 0; i2 < l; ++i2) {
            int j = this.value[i2];
            char c;
            do {
                final int k = j % 100;
                i += 2;
                c = SnmpOID.radixTenTenths[k];
                j /= 100;
            } while (j != 0);
            if (c == '0') {
                --i;
            }
        }
        return i;
    }
    
    @Override
    public Object getVarObject() {
        return this.toString();
    }
    
    @Override
    public int hashCode() {
        final int i = this.getCharacterCount();
        final char[] ac = this.toCharArray(i);
        int j = 0;
        int k = 1;
        for (int l = i + this.value.length, i2 = 0; i2 < l; ++i2) {
            j = 31 * j + ac[k++];
        }
        return j;
    }
    
    static SnmpOID newInstance(final int[] ai) {
        final SnmpOID snmpoid = new SnmpOID();
        snmpoid.Type = 6;
        snmpoid.value = ai;
        return snmpoid;
    }
    
    private void setOID(String s) {
        try {
            if (s.charAt(0) != '.') {
                s = SnmpAPI.Standard_Prefix + s;
            }
            final char[] ac = s.toCharArray();
            int i = 0;
            int j = ac.length;
            if (ac[i] == '.') {
                ++i;
            }
            if (ac[j - 1] == '.') {
                --j;
            }
            int k = 1;
            for (int l = i; l < j; ++l) {
                if (ac[l] == '.') {
                    ++k;
                }
            }
            this.value = new int[k];
            int i2 = 0;
            int j2 = 0;
            final boolean flag = false;
            for (int l2 = i; l2 < j; ++l2) {
                if (ac[l2] == '.') {
                    this.value[i2++] = (int)((long)j2 & 0xFFFFFFFFL);
                    j2 = 0;
                }
                else {
                    final int k2;
                    if ((k2 = Character.digit(ac[l2], 10)) < 0) {
                        throw new Exception();
                    }
                    j2 = j2 * 10 + k2;
                }
            }
            this.value[i2] = (int)((long)j2 & 0xFFFFFFFFL);
        }
        catch (Exception _ex) {
            System.err.println(SnmpUtils.getString("Invalid OID format: ") + " " + s);
            this.value = null;
        }
    }
    
    @Override
    public byte[] toBytes() {
        if (this.byteValue == null) {
            final int i = this.value.length;
            this.byteValue = new byte[4 * i];
            for (int j = 0; j < i; ++j) {
                int k = this.value[j];
                int l = (j + 1) * 4;
                while (k != 0) {
                    this.byteValue[--l] = (byte)(k & 0xFF);
                    k >>>= 8;
                }
            }
        }
        return this.byteValue;
    }
    
    private char[] toCharArray(final int i) {
        final char[] ac = new char[i + 1 + this.value.length];
        int j = i + 1 + this.value.length;
        final boolean flag = false;
        final int l = this.value.length;
        for (int i2 = l - 1; i2 >= 0; --i2) {
            int k = this.value[i2];
            do {
                final int j2 = k % 100;
                ac[--j] = SnmpOID.radixTenUnits[j2];
                ac[--j] = SnmpOID.radixTenTenths[j2];
                k /= 100;
            } while (k != 0);
            if (ac[j] == '0') {
                ++j;
            }
            ac[--j] = '.';
        }
        return ac;
    }
    
    public int[] toIntArray() {
        final int[] ai = new int[this.value.length];
        System.arraycopy(this.value, 0, ai, 0, this.value.length);
        return ai;
    }
    
    public long[] toLongArray() {
        return this.convertIntArrayToLongArray(this.value);
    }
    
    @Override
    public String toString() {
        if (this.value == null) {
            return null;
        }
        final StringBuffer stringbuffer = new StringBuffer();
        for (int j = this.value.length, i = 0; i < j; ++i) {
            long l = (long)this.value[i] & 0x7FFFFFFFL;
            if (this.value[i] < 0) {
                l += 2147483648L;
            }
            stringbuffer.append("." + Long.toString(l));
        }
        return stringbuffer.toString().intern();
    }
    
    @Override
    public String toTagString() {
        return new String("Object ID : " + this.toString());
    }
    
    @Override
    public Object toValue() {
        if (this.value == null) {
            return null;
        }
        return this.toIntArray();
    }
    
    static {
        SnmpOID.zeroDotZero = new int[2];
        radixTenTenths = new char[] { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '9', '9', '9', '9', '9', '9', '9', '9', '9', '9' };
        radixTenUnits = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    }
}
